/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: AssetCategoryController
 * Author:   adi
 * Date:     2018/12/5 10:57
 * Description: 资产类别管理控制器
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */

package com.dnzx.controller.common;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.dnzx.framework.page.PageResult;
import org.dnzx.framework.result.ResultUtils;
import org.dnzx.framework.result.domain.Result;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dnzx.client.IDictClient;
import com.dnzx.model.DictBO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 〈一句话功能简述〉<br>
 * 〈字典表管理控制器〉
 *
 * @author adi
 * @create 2018/12/5
 * @since 1.0.0
 */
@RestController
@RequestMapping("/dict")
@Api(value = "/dict", description = "字典表管理控制器", produces = MediaType.APPLICATION_JSON_VALUE)
public class DictController {

	@Resource
	private IDictClient dictClient;
	
	/**
	 * 新增
	 * @param dictBO
	 * @return
	 */
	@PostMapping(value = "/save")
    @ApiOperation(value = "/save",notes = "新增",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result save(@ApiParam(value = "实体") @RequestBody @Valid DictBO dictBO){
		try{
			this.dictClient.save(dictBO);
	        return ResultUtils.returnSuccess("SUCCESS");
        }catch (Exception e){
            return ResultUtils.returnError("ERROR >>> " + e.getMessage());
        }
    }
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete/{id}")
	@ApiOperation(value = "/delete/{id}",notes = "删除",httpMethod = "DELETE",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result delete(@ApiParam(value = "id") @PathVariable("id") Long id){
		try {
			this.dictClient.delete(id);
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 修改
	 * @param dictBO
	 * @return
	 */
	@PutMapping(value = "/update")
	@ApiOperation(value = "/update",notes = "修改",httpMethod = "PUT",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result update(@ApiParam(value = "实体") @RequestBody DictBO dictBO) {
		try {
			this.dictClient.update(dictBO); 
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 查看
	 * @param id
	 * @return
	 */
	@GetMapping("/get/{id}")
	@ApiOperation(value = "/get/{id}",notes = "查看",httpMethod = "GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result get(@ApiParam(value = "id") @PathVariable("id") Long id) {
		try {
			DictBO dictBO = this.dictClient.get(id);
			return ResultUtils.returnSuccess("SUCCESS", dictBO);
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}

	/**
	 * 查询 - 分页
	 * @param dictBO
	 * @return
	 */
	@PostMapping("/queryPage")
	@ApiOperation(value="/queryPage",notes="查询 - 分页",httpMethod="POST",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryPage(@ApiParam(value = "实体") @RequestBody DictBO dictBO) {
		try {
			PageResult<List<DictBO>> pageResult = new PageResult<List<DictBO>>();
			pageResult.setCurrentPage(dictBO.getCurrentPage());
			pageResult = this.dictClient.queryPage(dictBO, pageResult);
			return ResultUtils.returnSuccess("SUCCESS", pageResult);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 查询 - 树（用于菜单展示和权限分配）
	 * @param roleDictionaryBO
	 * @return
	 */
	@GetMapping("/queryList/{type}")
	@ApiOperation(value="/queryList/{type}",notes="查询 - 树",httpMethod="GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryList(@ApiParam(value = "type") @PathVariable("type") String type) {
		try {
			DictBO dictBO = new DictBO();
			dictBO.setType(type);
			List<DictBO> dictBOList = this.dictClient.queryList(dictBO);
			return ResultUtils.returnSuccess("SUCCESS", dictBOList);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}

}
